<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="queryParams.studentId" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="住宿地址" prop="lodging">
        <el-input v-model="queryParams.lodging" placeholder="请输入住宿地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="宿舍楼" prop="dormitory">
        <el-input v-model="queryParams.dormitory" placeholder="请输入宿舍楼" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="房间号" prop="room">
        <el-input v-model="queryParams.room" placeholder="请输入房间号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="床位号" prop="bed">
        <el-input v-model="queryParams.bed" placeholder="请输入床位号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="宿舍价位" prop="roomLevel">
        <el-select v-model="queryParams.roomLevel" placeholder="请选择宿舍价位" clearable>
          <el-option v-for="dict in dict.type.reg_room_level" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['stureg:checkroom:add']">选择宿舍</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['stureg:checkroom:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['stureg:checkroom:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['stureg:checkroom:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="住宿地址" align="center" prop="lodging" />
      <el-table-column label="宿舍楼" align="center" prop="dormitory" />
      <el-table-column label="房间号" align="center" prop="room" />
      <el-table-column label="床位号" align="center" prop="bed" />
      <el-table-column label="宿舍价位" align="center" prop="roomLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reg_room_level" :value="scope.row.roomLevel" />
        </template>
      </el-table-column>
      <el-table-column label="部门状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['stureg:checkroom:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['stureg:checkroom:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改入住信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentId">
          <el-select v-model="form.studentId" filterable placeholder="请选择学生" @change="changeLocationValue">
            <el-option v-for="item in stuSelect" :key="item.registerId" :label="item.registerId + '--' + item.userName"
              :value="item.registerId">
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.studentId" :disabled="true" /> -->
        </el-form-item>
        <!-- <el-form-item label="住宿地址" prop="lodging">
          <el-input v-model="form.lodging" placeholder="请输入住宿地址" />
        </el-form-item>
        <el-form-item label="宿舍楼" prop="dormitory">
          <el-input v-model="form.dormitory" placeholder="请输入宿舍楼" />
        </el-form-item>
        <el-form-item label="房间号" prop="room">
          <el-input v-model="form.room" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="床位号" prop="bed">
          <el-input v-model="form.bed" placeholder="请输入床位号" />
        </el-form-item> -->
        <el-form-item class="roomlevel" label="宿舍价位" prop="roomLevel">
          <el-select v-model="form.roomLevel" placeholder="请选择宿舍价位" @change="changeRoomLevel">
            <el-option v-for="dict in dict.type.reg_room_level" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" ></el-option>
          </el-select>
          
        </el-form-item>
        <el-form-item label="宿舍照片">
          <!-- <div class="demo-image__preview">
            
          </div> -->
          <el-image style="width: 400px; height: 400px" :src="url" >
            </el-image>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckroom, getCheckroom, delCheckroom, addCheckroom, updateCheckroom } from "@/api/stureg/checkroom";
import { listInfo } from "@/api/stureg/intenetin";

export default {
  name: "Checkroom",
  dicts: ['reg_room_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入住信息表格数据
      checkroomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        studentId: null,
        lodging: null,
        dormitory: null,
        room: null,
        bed: null,
        roomLevel: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //选择学生的下拉框数据
      stuSelect: [],
      // 宿舍图片
      uuL:"",
      url: '',     
      srcList: [
        require('@/assets/roomimg/img3.png'),
        require('@/assets/roomimg/img2.png'),
        require('@/assets/roomimg/img1.png'),
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询入住信息列表 */
    getList() {
      this.loading = true;
      listCheckroom(this.queryParams).then(response => {
        this.checkroomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        studentId: null,
        lodging: null,
        dormitory: null,
        room: null,
        bed: null,
        roomLevel: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //加载学生下拉框
    async initStuSelect() {
      let res = await listInfo();
      this.stuSelect = [...res.rows];
    },
    //获取下拉框的值
    changeLocationValue(val) {
      //locations是v-for里面的也是datas里面的值
      console.log(val);
      let obj = {};
      obj = this.stuSelect.find((item) => {
        return item.registerId === val;
      });
      let getName = ''
      getName = obj.userName;
      this.form.name = getName; //将表单里的学生姓名双向绑定
    },
    changeRoomLevel(val){
      if(val=="3"){
        this.url=this.uuL+this.srcList[0]
      }else if(val=="2"){
        this.url=this.uuL+this.srcList[1]
      }else{
        this.url=this.uuL+this.srcList[2]
      }
    },
    /** 新增按钮操作 */
    async handleAdd() {
      await this.initStuSelect();
      this.reset();
      this.open = true;
      this.title = "添加入住信息";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      await this.initStuSelect();
      this.reset();
      const id = row.id || this.ids
      getCheckroom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入住信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheckroom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckroom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除入住信息编号为"' + ids + '"的数据项？').then(function () {
        return delCheckroom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stureg/checkroom/export', {
        ...this.queryParams
      }, `checkroom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.roomlevel {
  position: relative;
}

.demo-image__preview {
  position: absolute;
  right: 26px;
  top: -13px;

  .el-image {
    width: 142px;
    height: 98px;
    border-radius: 12px;
  }
}
</style>